package com.flow.demo.module.gd.controller.app.list;

import com.flow.demo.framework.web.core.util.WebFrameworkUtils;
import com.flow.demo.module.gd.controller.app.list.dto.GoodMySelfDTO;
import com.flow.demo.module.gd.controller.app.list.vo.ListEditVO;
import com.flow.demo.module.gd.controller.app.list.vo.ListPageReqVO;
import com.flow.demo.module.gd.controller.app.list.vo.ListRespVO;
import com.flow.demo.module.gd.controller.app.list.vo.ListSaveReqVO;
import com.flow.demo.module.gd.controller.app.user.dto.FullCondition;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.flow.demo.framework.common.pojo.PageParam;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.util.object.BeanUtils;

import static com.flow.demo.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
import static com.flow.demo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.flow.demo.framework.common.pojo.CommonResult.success;

import com.flow.demo.framework.excel.core.util.ExcelUtils;

import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import static com.flow.demo.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.flow.demo.module.gd.dal.dataobject.list.ListDO;
import com.flow.demo.module.gd.service.list.ListService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 用户获奖名单")
@RestController
@RequestMapping("/gd/list")
@Validated
public class ListController {

    @Resource
    private ListService listService;

    @PostMapping("/create")
    @Operation(summary = "创建用户获奖名单")
    public CommonResult<Integer> createList(@Valid @RequestBody ListSaveReqVO createReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        createReqVO.setGdUserId(userId.intValue());
        return success(listService.createList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户获奖名单")
    public CommonResult<Boolean> updateList(@Valid @RequestBody ListEditVO updateReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        updateReqVO.setGdUserId(userId.intValue());
        listService.updateList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户获奖名单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gd:list:delete')")
    public CommonResult<Boolean> deleteList(@RequestParam("id") Integer id) {
        listService.deleteList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户获奖名单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gd:list:query')")
    public CommonResult<ListRespVO> getList(@RequestParam("id") Integer id) {
        ListDO list = listService.getList(id);
        return success(BeanUtils.toBean(list, ListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户获奖名单分页")
    @PreAuthorize("@ss.hasPermission('gd:list:query')")
    public CommonResult<PageResult<ListRespVO>> getListPage(@Valid ListPageReqVO pageReqVO) {
        PageResult<ListDO> pageResult = listService.getListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户获奖名单 Excel")
    @PreAuthorize("@ss.hasPermission('gd:list:export')")
    @OperateLog(type = EXPORT)
    public void exportListExcel(@Valid ListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ListDO> list = listService.getListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户获奖名单.xls", "数据", ListRespVO.class,
                        BeanUtils.toBean(list, ListRespVO.class));
    }


    @PostMapping("/goodMySelf")
    @Operation(summary = "验证是否有机会抽奖")
    public CommonResult<GoodMySelfDTO> getGoodMySelfHandler() {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if(userId == null)
            throw exception(UNAUTHORIZED);
        return success(listService.getGoodMySelf(userId));
    }

}
